﻿using System;
using System.IO;

using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.UI.UserControls;




namespace DotNetNuke.Modules.UserDefinedTable
{



  public class EditDownload : EditURL
  {



    //public EditDownload()
    //{
    //  base.Load += new EventHandler(this.EditDownload_Load);
    //}


    protected override void OnLoad(EventArgs e)
    {
      base.OnLoad(e);
      //base.Load += new EventHandler(this.EditDownload_Load);

      if (!this.Page.IsPostBack && this.IsNotAListOfValues)
      {
        UrlControl ctlURL = base.ctlURL;
        ctlURL.ShowUrls = false;
        ctlURL.ShowTabs = false;
        ctlURL.ShowSecure = true;
        ctlURL.ShowDatabase = true;
        ctlURL.ShowTrack = true;
        ctlURL = null;
      }
    }




    //private void EditDownload_Load(object sender, EventArgs evt)
    //{
    //  if (!this.Page.IsPostBack && this.IsNotAListOfValues)
    //  {
    //    UrlControl ctlURL = base.ctlURL;
    //    ctlURL.ShowUrls = false;
    //    ctlURL.ShowTabs = false;
    //    ctlURL.ShowSecure = true;
    //    ctlURL.ShowDatabase = true;
    //    ctlURL.ShowTrack = true;
    //    ctlURL = null;
    //  }
    //}





    public override string Value
    {
      get
      {
        if (this.IsNotAListOfValues)
        {
          new UrlController().UpdateUrl(EditControl.PortalID, base.ctlURL.Url, base.ctlURL.UrlType, base.ctlURL.Log, base.ctlURL.Track, base.ctlURL.ModuleID, base.ctlURL.NewWindow);
          return (base.ctlURL.Url + (base.ctlURL.NewWindow ? "|options=W" : ""));
        }
        FileController controller2 = new FileController();
        FolderController controller3 = new FolderController();
        string path = Utilities.CStrN(base.ctlValueBox.SelectedValue, "");
        string fileName = Path.GetFileName(path);
        FolderInfo info2 = new FolderInfo();
        info2 = controller3.GetFolder(EditControl.PortalID, path.Replace(fileName, ""), false);
        DotNetNuke.Services.FileSystem.FileInfo expression = new DotNetNuke.Services.FileSystem.FileInfo();
        expression = controller2.GetFile(fileName, EditControl.PortalID, info2.FolderID);

        if (expression != null)
          return string.Format("FileID={0}", expression.FileId);

        return "";
      }
      set
      {
        if (this.IsNotAListOfValues)
        {
          base.ctlURL.Url = URLUtil.StripURL(ref value);
          this.ValueIsSet = true;
        }
        else
        {
          try
          {
            base.ctlValueBox.SelectedValue = value;
            this.ValueIsSet = true;
          }
          catch (Exception e)
          {


          }
        }
      }
    }



  }

}

